﻿using mars_server.csharp.AO;
using mars_server.csharp.AO.impl;
using mars_server.csharp.CO;
using mars_server.csharp.dao;
using mars_server.csharp.dao.impl;
using mars_server.csharp.enums;
using mars_server.csharp.model;
using mars_server.csharp.socket;
using mars_server.csharp.Util;
using mars_server.csharp.VO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mars_server.csharp.controller
{
    class ChatController
    {
        private ChatController() { }
        private static readonly ChatController instance = new ChatController();

        internal static ChatController Instance
        {
            get
            {
                return instance;
            }
        }


        UserManager userManager = UserManagerImpl.Instance;

        UserSocketAO userSocketAO = UserSocketAOImpl.Instance;

        UserGroupAO userGroupAO = UserGroupAOImpl.Instance;

        UserAO userAO = UserAOImpl.Instance;

        RelationAO relationAO = RelationAOImpl.Instance;

        /***
         * 单聊
         * */
        public void sendSingleChatMsg(MessageCO messageCO,UserSocket userSocket) 
        {
            SingleChatVO singleChatVO  = JsonUtil.j2o<SingleChatVO>(messageCO.content);
            UserDO userDO = userSocket.userDO;
            UserVO senderVO = new UserVO();
            UserVO receiverVO = new UserVO();

            if (!relationAO.isFriendRelation(singleChatVO.senderVO.id, singleChatVO.receiverVO.id))//如果两者不是好友关系
            {
                singleChatVO.messageVO = null;
                messageCO.content = singleChatVO;
                userSocketAO.sendMessageToSocket(userSocket, messageCO);//发送给发起者
                return;

            }
            UserDO receiverDO = userManager.selectByPrimaryKey(singleChatVO.receiverVO.id);
            if (!userSocketAO.isUserOnlie(receiverDO.id))
            {
                senderVO = DOVOutil.userDO2VO(userDO);
                singleChatVO.senderVO = senderVO;
                singleChatVO.messageVO = null;
                messageCO.content = singleChatVO;
                userSocketAO.sendMessageToSocket(userSocket, messageCO);
                return;
            }
            receiverVO = DOVOutil.userDO2VO(receiverDO);
            senderVO = DOVOutil.userDO2VO(userDO);

            singleChatVO.receiverVO = receiverVO;
            singleChatVO.sendTime= DateTime.Now.ToString();
            singleChatVO.senderVO = senderVO;
            messageCO.content = singleChatVO;


            userSocketAO.sendMessageToUser(receiverDO, messageCO);//发送给接收者
            userSocketAO.sendMessageToUser(userDO, messageCO);//发送给发起者
        }

        public void sendGroupChatMsg(MessageCO messageCO, UserSocket userSocket)
        {
            GroupChatVO groupChatVO = JsonUtil.j2o<GroupChatVO>(messageCO.content);
            GroupVO groupVO = groupChatVO.groupVO;
            UserVO userVO = groupChatVO.senderVO;
            UserDO senderDO = userManager.selectByPrimaryKey(userVO.id);
            groupChatVO.senderVO = DOVOutil.userDO2VO(senderDO);//把发送者的UserDO数据封装进去
            groupChatVO.sendTime = DateTime.Now.ToString();//当前时间
            messageCO.content = groupChatVO;
            List<UserDO> userDOs = userGroupAO.queryUserDOsByGroupId(groupVO.id);//群内的所有用户
            foreach(UserDO userDO in userDOs)
            {
                if (userSocketAO.isUserOnlie(userDO.id))
                {
                    Console.WriteLine("[info]|--【群聊】--群聊信息分发 ["+userDO.nickName+"]");
                    userSocketAO.sendMessageToUser(userDO, messageCO);
                }
            }
        }
    }
}
